import { Router } from "express";
import { IcallbackFn, Ioracle } from "../../types";
import multer from "multer";
import fs from 'fs'

const storage = multer.diskStorage({
  //上传文件到服务器的存储位置
  destination: "../uploadFile/CustomAR",
  filename: function (req, file, callback) {
    //上传的文件信息
    let type: string = typeof req;
    type = type + "tempSource";
    // 用來獲取後綴名
    let fileFormat = file.originalname.split(".");
    // 获取时间戳
    // 文件的命名为：时间戳 + 文件附件名 + 文件的后缀名
    callback(
      null,
      fileFormat[0] + "." + fileFormat[fileFormat.length - 1]
    );
  },
});
const upload = multer({
  storage: storage,
});

//切片文件上傳，使用此接口。
const storageSawPieces  =  multer.diskStorage({
  //上传文件到服务器的存储位置
  destination: "../uploadFile/SawPieces",
  filename: function (req, file, callback) {
    //上传的文件信息
    let type: string = typeof req;
    type = type + "tempSource";
    // 用來獲取後綴名
    let fileFormat = file.originalname.split(".");
    // 获取时间戳
    // 文件的命名为：时间戳 + 文件附件名 + 文件的后缀名
    callback(
      null,
      fileFormat[0] + "." + fileFormat[fileFormat.length - 1]
    );
  },
});
const uploadSawPieces = multer({
  storage: storageSawPieces,
});

export default (router: Router, oracle: Ioracle, callbackFn: IcallbackFn) => {
  router.post("/upload", upload.single("file"), (req, res) => {
    const { docid,filename } = req.body;
    let sql = ` update xx_cf_customar_apply_header set appendix_url ='${filename}'`
    sql += ` where docid='${docid}'`
    callbackFn(res,oracle.connChaos,{sql});
  });

  router.post("/download",(req,res)=>{
    const {filename} = req.body
    const filePath = '../uploadFile/CustomAR/' + filename
    const f = fs.createReadStream(filePath);
    const content_type = filename.split('.')[filename.split('.').length -1] === 'xlsx' ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    : 'application/octet-stream'
    res.writeHead(200, {
        //'Content-Type': 'application/force-download',
          'Content-Type': content_type,
          'Content-Disposition': 'attachment; filename=' + filename,
          'Access-Control-Expose-Headers': 'Content-Disposition',//核心配置
    });
    f.pipe(res);
  })

  router.post("/uploadSawPieces", uploadSawPieces.single("file"), (req,res) => {
    const { filename,rowid } = req.body;
    let sql = ` update cm_wip_knife_history set appendix = '${filename}'`
    sql += ` where rowid = '${rowid}'`
    callbackFn(res,oracle.connCtShopFloor,{sql});
  });

  router.post("/downloadSawPieces",(req,res)=>{
    const {filename} = req.body
    const filePath = '../uploadFile/SawPieces/' + filename
    const f = fs.createReadStream(filePath);
    const content_type = filename.split('.')[filename.split('.').length -1] === 'xlsx' ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    : 'application/octet-stream'
    res.writeHead(200, {
        //'Content-Type': 'application/force-download',
          'Content-Type': content_type,
          'Content-Disposition': 'attachment; filename=' + filename,
          'Access-Control-Expose-Headers': 'Content-Disposition',//核心配置
    });
    f.pipe(res);
  })

  return router;
};
